package com.dangf.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dangf.entity.Article;
import com.dangf.service.ArticleService;
import com.dangf.utils.IsNullUtils;

/**
 * 文章控制器
 * 
 * @author HaiSheng
 * @date 2015年7月13日
 */

@Controller
@RequestMapping("/article")
public class ArticleController extends BaseController {

	@Resource
	private ArticleService articleService;

	@RequestMapping("")
	@ResponseBody
	// 列表
	public Map<String, Object> index(int start, int count) {
		if (count == 0) {
			return setResult(HTTP_CODE_FAILED, "请输入要查询的数量！", "");
		} else {
			List<Article> list = articleService.getListByPage(start, count);
			return setResult(HTTP_CODE_SUCCESS, "查询成功！", list);
		}

	}

	@RequestMapping("/add")
	@ResponseBody
	// 列表
	public Map<String, Object> add(Article article) {
		if (article != null && !"".equals(article.getTitle()) && article.getType()!=null) {
			if (article.getType().getId() == 0){
				return setResult(HTTP_CODE_FAILED, "添加失敗，文章分類不能為空！", "");
			}else if(!IsNullUtils.stringIsNull(article.getContent())){
				return setResult(HTTP_CODE_FAILED, "文章內容不能為空！", "");
			}else if (articleService.save(article) != null) {
				return setResult(HTTP_CODE_SUCCESS, "文章添加成功！", "");
			} else {
				return setResult(HTTP_CODE_FAILED, "文章添加失败！", "");
			}
		} else {
			return setResult(HTTP_CODE_FAILED, "添加失败，信息不能为空！", "");
		}
	}

}
